package com.dangdi.util;

import util.gFunction;

import java.util.HashMap;
import java.util.Map;

public class RfidWrite {
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("book_name", "锋利的Jquery（第二版本）");
//        map.put("book_publisher","人民邮电出版社");
//        map.put("book_writer","单东林，张晓菲，巍然，编者");
//        map.put("book_price","13.56");
//        map.put("book_weizhi","1号书架A面1层1列");
        System.out.println(info(map));
    }

    public static String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "UTF-8");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    public static Map<String, Object> info(Map<String, Object> m) {
        System.out.println("原始数据=" + m);
        String s = "$" + m.get("book_name") + "#$" + m.get("book_publisher") + "#$" + m.get("book_writer") + "#$" + m.get("book_price") + "#";
        String st = gFunction.encodeHexStr(s.getBytes());
        System.out.println("转换后的数据=" + st);
        System.out.println("转换后的数据长度：" + st.length());
        System.out.println("所需要的字节=" + st.length() / 8);
        Map<String, Object> map = new HashMap<>();
        String ttt = hexStringToString(st);
        int index = s.indexOf("#");
        map.put("bookName", ttt.substring(1, index));
        ttt = ttt.substring(index + 1, ttt.length());
        index = ttt.indexOf("#");
        map.put("house", ttt.substring(1, index));
        ttt = ttt.substring(index + 1, ttt.length());
        index = ttt.indexOf("#");
        map.put("author", ttt.substring(1, index));
        ttt = ttt.substring(index + 1, ttt.length());
        index = ttt.indexOf("#");
        map.put("price", ttt.substring(1, index));
//        ttt=ttt.substring(index+1,ttt.length());
//        index=ttt.indexOf("#");
//        map.put("book_location",ttt.substring(1,index));
//        s=s.substring(index,s.length());
//        index=s.indexOf("#");
        return map;
    }
}